package com.wgj.android.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.jiaai.xui.XUI;
import com.jiaai.xui.cache.XCache;
import com.jiaai.xui.utils.XPreferencesUtils;
import com.jiaai.xui.utils.log.XLog;
import com.wgj.android.entity.Member;
import com.wgj.android.entity.SystemConfg;
import com.wgj.android.entity.Version;
import com.wgj.android.entity.YoukuToken;
import com.wgj.android.http.model.AccessToken;
import com.wgj.android.ui.activity.MainActivity;

/**
 * Created by Administrator on 2017/10/31.
 */

public class CacheUtils  {
    private  static  final  String TOKEN_CACHE="token_cache";
    private  static  final  String YOUKU_TOKEN="youku_token";
    private static  final  String MEMBER_CACH_KEY="member_cach";
    private  static  final String SYSTEM_CONFIG_CACH="system_config_cach";

    public  static  void logout(){
        try {
            XCache.get(XUI.getContext()).remove(TOKEN_CACHE);
            XCache.get(XUI.getContext()).remove(TOKEN_CACHE);
        }catch (Exception e){
            XLog.e("清除缓存异常");
        }

    }

    public  static void cacheToken(AccessToken accessToken){
       XCache.get(XUI.getContext()).put(TOKEN_CACHE,accessToken);
    }

    public  static AccessToken  getToken(){
       return (AccessToken) XCache.get(XUI.getContext()).getAsObject(TOKEN_CACHE);
    }
    public  static void cacheYouKuToken(String accessToken){
       XCache.get(XUI.getContext()).put(YOUKU_TOKEN,accessToken);
    }

    public  static String getYouKuToken(){
       return XCache.get(XUI.getContext()).getAsString(YOUKU_TOKEN);
    }


    public  static Member getMember(){
        return (Member) XCache.get(XUI.getContext()).getAsObject(MEMBER_CACH_KEY);
    }
    public  static void setMember(Member member)throws  Exception {
        try {
            XCache.get(XUI.getContext()).put(MEMBER_CACH_KEY,member);
        }catch (Exception e){
            throw new Exception("写入失败");
        }

    }


    public static SystemConfg getSystemConfig(){
        return (SystemConfg) XCache.get(XUI.getContext()).getAsObject(SYSTEM_CONFIG_CACH);
    }

    public  static  void  setSystemConfig(SystemConfg config){
        XCache.get(XUI.getContext()).put(SYSTEM_CONFIG_CACH,config);
    }

    public  static Version getVersion(Context context){
        return (Version) XCache.get(context).getAsObject("APP_VERSION");
    }

    public  static void setVersion(Context context,Version version){
         XCache.get(context).put("APP_VERSION",version);
    }
}
